<template>
  <div>
    <div style="height: 20px">&nbsp;</div>
    <div class="container">
      <div class="handle-box">
        <el-button
            type="primary"
            icon="el-icon-group"
            class="handle-del mr10"
            @click="delAllSelection"
        >选中按钮
        </el-button>

      </div>
      <el-table
          :data="PetData"
          border
          class="table"
          ref="multipleTable"
          header-cell-class-name="table-header"
          @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="55" align="center"></el-table-column>
        <el-table-column prop="id" label="ID" width="55" align="center"></el-table-column>
        <el-table-column prop="name" label="宠物名" width="100" align="center"></el-table-column>
        <el-table-column label="年龄" width="100" align="center">
          <template slot-scope="scope">{{ scope.row.age }}个月</template>
        </el-table-column>
        <el-table-column label="头像(查看大图)" width="100" align="center">
          <template slot-scope="scope">
            <el-image
                class="table-td-thumb"
                :src="scope.row.picture"
                :preview-src-list="[scope.row.picture]"
            ></el-image>
          </template>
        </el-table-column>
        <el-table-column prop="address" label="领养地址" width="400" align="center"></el-table-column>
        <el-table-column label="验证情况" width="100" align="center">
          <template slot-scope="scope">
            <el-tag
                :type="scope.row.ifVerify=== '已验证' ?'success':(scope.row.ifVerify=== '未验证'?'danger':'')"
            >{{ scope.row.ifVerify }}
            </el-tag>
          </template>
        </el-table-column>

        <el-table-column label="领养情况" width="100" align="center">
          <template slot-scope="scope">
            <el-tag
                :type="scope.row.ifAdopt=== '已领养' ?'success':(scope.row.ifAdopt=== '未领养'?'danger':'')"
            >{{ scope.row.ifAdopt }}
            </el-tag>
          </template>
        </el-table-column>

        <el-table-column prop="introduction" label="简介" align="center"></el-table-column>
        <el-table-column label="操作" width="180" align="center">
          <template slot-scope="scope">
            <el-button
                type="text"
                icon="el-icon-edit"
                @click="handleEdit(scope.$index, scope.row)"
            >编辑
            </el-button>
            <el-button
                type="text"
                icon="el-icon-delete"
                class="red"
                @click="handleDelete(scope.$index, scope.row)"
            >删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="pagination">
        <el-pagination
            background
            layout="total, prev, pager, next"
            :current-page="current"
            :page-size="query.pageSize"
            :total="totlalElement"
            @current-change="handlePageChange"
        ></el-pagination>
      </div>
    </div>

    <!-- 编辑弹出框 -->
    <el-dialog title="编辑" :visible.sync="editVisible" width="30%">
      <el-form ref="form" :model="form" label-width="70px">
        <el-form-item label="宠物名">
          <el-input v-model="form.name"></el-input>
        </el-form-item>
        <el-form-item label="年龄">
          <el-input v-model="form.age"></el-input>
        </el-form-item>
        <el-form-item label="领养地址">
          <el-input v-model="form.address"></el-input>
        </el-form-item>
        <el-form-item label="简介">
          <el-input
              type="textarea"
              resize="none"
              :autosize="{ minRows: 4, maxRows: 4 }"
              placeholder="请输入意见"
              v-model="form.introduction"/>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
                <el-button @click="editVisible = false">取 消</el-button>
                <el-button type="primary" @click="saveEdit">确 定</el-button>
            </span>
    </el-dialog>
  </div>
</template>

<script>
import {fetchData} from '../../api/index';
import axios from "axios";

export default {
  name: 'test1',
  data() {
    return {
      query: {
        pageIndex: 1,
        pageSize: 10
      },
      current: 1,
      totlalElement: 0,
      ifAdopt: "",
      name: "",
      real_size: 0,
      PetData: "",
      tableData: [],
      multipleSelection: [],
      delList: [],
      editVisible: false,
      pageTotal: 0,
      form: {},
      idx: -1,
      id: -1
    };
  },
  created() {
    /*this.getData();*/
    this.showPets()
  },
  methods: {
    // 获取 easy-mock 的模拟数据
    getData() {
      fetchData(this.query).then(res => {
        console.log(res);
        this.tableData = res.list;
        this.pageTotal = res.pageTotal || 50;
      });
    },
    // 触发搜索按钮
    handleSearch() {
      this.showPets()
    },
    // 删除操作
    handleDelete(index, row) {
      // 二次确认删除
      this.$confirm('确定要删除吗？', '提示', {
        type: 'warning'
      })
          .then(() => {
            axios
                .delete(this.$store.state.url+":8412/pet/deletePetById?id=" + row.id)
                .then((data) => {
                  console.log("删除宠物中。。。")
                })
                .catch((err) => {
                  console.log(err);
                });
            this.$message.success('删除成功');
            this.current = 1;
            this.showPets();
          })
    },
    // 多选操作
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    delAllSelection() {
      const length = this.multipleSelection.length;
      let str = '';
      this.delList = this.delList.concat(this.multipleSelection);
      for (let i = 0; i < length; i++) {
        str += this.multipleSelection[i].name + ' ';
      }
      this.$message.success(`选中了${str}`);
      /*this.multipleSelection = [];*/
    },
    // 编辑操作
    handleEdit(index, row) {
      this.idx = index;
      this.form = row;
      this.editVisible = true;
    },
    // 保存 修改时候的编辑
    saveEdit() {
      this.editVisible = false;
      this.$message.success(`修改本页第 ${this.idx + 1} 行成功`);
      axios
          .post(this.$store.state.url+":8412/pet/updatePet?id=" + this.form.id +
              "&name=" + this.form.name + "&age=" + this.form.age +
              "&address=" + this.form.address + "&introduction=" + this.form.introduction)
          .then((data) => {
            console.log("修改宠物中。。。")
          })
          .catch((err) => {
            console.log(err);
          });
      this.$message.success('修改成功');
      this.showPets();
    },
    // 分页导航
    handlePageChange(val) {
      this.current = val
      this.showPets()
      console.log(`当前页: ${val}`);
    },
    showPets() {
      let str = ""
      if (this.name != "") {
        console.log(this.name)
        str = str + "&name=" + this.name;
      }
      if (this.ifAdopt != "" && this.ifAdopt != '全部') {
        str = str + "&ifAdopt=" + this.ifAdopt;
      }
      axios
          .post(this.$store.state.url+":8412/pet/showUserSendPetMsg?" +
              "&id=" + localStorage.getItem("myId") + "&current="
              + this.current + "&size=" + this.query.pageSize + str)
          .then((data) => {
            console.log("查找宠物中。。。")
            console.log(data);
            this.PetData = data.data.data
            this.totalPage = data.data.totalPage
            this.totlalElement = data.data.totalElement
            if (this.totlalElement == 0) {
              alert("没有查到任何资料")
              this.real_size = 0;
            }
            this.real_size = data.data.data.length
            console.log(this.PetData)
          })
          .catch((err) => {
            console.log(err);
          });
    },
  }
};
</script>

<style scoped>
.handle-box {
  margin-bottom: 20px;
}

.table {
  width: 100%;
  font-size: 14px;
}

.red {
  color: #ff0000;
}

.mr10 {
  margin-right: 10px;
}

.table-td-thumb {
  display: block;
  margin: auto;
  width: 40px;
  height: 40px;
}
</style>
